﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using PaymentHelper.Methods.PaypalStandard.Models;
using PaymentHelper.Core.MVCUtils;

namespace PaymentHelper.Methods.PaypalStandard.Controlers
{
    public class PaypalStandardController:PaymentHelperController
    {
        protected override string SystemName
        {
            get { return "PaymentHelper.Methods.PaypalStandard"; }
        }

        [ChildActionOnly]
        public ActionResult Configure()
        {
            var settings = PaymentManager.Instance.LoadSettings(SystemName);

            var configModel = new ConfigModel();
            configModel.InitModel(settings);

            return View("PaymentHelper.Methods.PaypalStandard.Views.PaypalStandard.Configure",configModel);
        }

        [HttpPost]
        [ChildActionOnly]
        public ActionResult Configure(ConfigModel model)
        {
            if (!ModelState.IsValid)
                return View("PaymentHelper.Methods.PaypalStandard.Views.PaypalStandard.Configure", model);

            var listAtrr = model.ToListAttribute();
            bool isSuccess = PaymentManager.Instance.SaveSettings(SystemName,listAtrr);

            if (isSuccess)
                ViewBag.PaypalStandardMessage = "Changes are saved successfully !";
            else
                ViewBag.PaypalStandardMessage = "Changes are saved unsuccessfully !";

            return View("PaymentHelper.Methods.PaypalStandard.Views.PaypalStandard.Configure", model);
        }

        [ChildActionOnly]
        public ActionResult PaymentInfo()
        {
            var method = PaymentManager.Instance.LoadPaymentMethodBySystemName(SystemName);
            ViewBag.Resources = method.Descriptor.GetDefaultLanguage();

            return PartialView("PaymentHelper.Methods.PaypalStandard.Views.PaypalStandard.PaymentInfo");
        }

        [HttpPost]
        public ActionResult Complete(FormCollection frmData)
        {
            string payer_id = frmData["payer_id"];
            string first_name = frmData["first_name"];
            string last_name = frmData["last_name"];
            string payment_date = frmData["payment_date"];
            string payment_status = frmData["payment_status"];
            string payment_fee = frmData["payment_fee"];
            string payment_gross = frmData["payment_gross"];
            string payment_currcency = frmData["mc_currency"];

            PaymentResult result = new ProcessPaymentResult();
            if (payment_status.Equals("Completed", StringComparison.InvariantCultureIgnoreCase))
            {
                result.PaymentStatus = Core.RelatedObject.PaymentStatus.Paid;
                result.Success = true;
            }
            else
            {
                result.PaymentStatus = Core.RelatedObject.PaymentStatus.Pending;
                result.Success = false;
            }

            PaymentManager.Instance.UpdateCurrentPaymentState(result);

            var model = new PaymentCompleteModel() { 
                PayerFirstName = first_name,
                PayerId = payer_id,
                PayerLastName = last_name,
                PaymentDate = payment_date,
                PaymentFee = payment_fee,
                PaymentGross = payment_gross,
                PaymentStatus = payment_status,
                PaymentCurrency = payment_currcency
            };

            var method = PaymentManager.Instance.LoadPaymentMethodBySystemName(SystemName);
            ViewBag.Resources = method.Descriptor.GetDefaultLanguage();

            var state = PaymentManager.Instance.GetCurrentPaymentState();
            ViewBag.ProcessResultUrl = state.UrlProcessResult;

            return View("PaymentHelper.Methods.PaypalStandard.Views.PaypalStandard.Complete",model);
        }

        public override IList<string> ValidatePaymentInfo(FormCollection frmData)
        {
            return null;
        }
    }
}
